package com.xbongbong.paas.enums.bi;

import com.xbongbong.paas.constant.XbbConstant;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import lombok.Getter;
import org.springframework.util.StringUtils;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * 业务和数据库的对应
 */
@Getter
public enum XbbTableRefEnum {
    /**
     *
     */
    PAAS(XbbRefTypeEnum.PAAS,"tb_paas_form_data", SubsidiaryEnum.MASTER.getCode(),0),
    PAAS_SUBFORM(XbbRefTypeEnum.PAAS_SUBFORM,"tb_paas_form_paas_sub_data", SubsidiaryEnum.SUB_DATA.getCode(),PAAS.code),
    PAAS_TEAM(XbbRefTypeEnum.PAAS_TEAM,"tb_paas_user", SubsidiaryEnum.USER.getCode(),PAAS.code),
    CUSTOMER_MANAGEMENT(XbbRefTypeEnum.CUSTOMER_MANAGEMENT,"tb_saas_customer",SubsidiaryEnum.MASTER.getCode(),0),
    CUSTOMER_TEAM(XbbRefTypeEnum.CUSTOMER_TEAM,"tb_saas_customer_user",SubsidiaryEnum.USER.getCode(),CUSTOMER_MANAGEMENT.code),
    CUSTOMER_SUBFORM(XbbRefTypeEnum.CUSTOMER_SUBFORM,"tb_saas_customer_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),CUSTOMER_MANAGEMENT.code),
    //
    CONTRACT(XbbRefTypeEnum.CONTRACT,"tb_saas_contract",SubsidiaryEnum.MASTER.getCode(),0),
    CONTRACT_TEAM(XbbRefTypeEnum.CONTRACT_TEAM,"tb_saas_contract_user",SubsidiaryEnum.USER.getCode(),CONTRACT.code),
    CONTRACT_SUBFORM(XbbRefTypeEnum.CONTRACT_SUBFORM,"tb_saas_contract_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),CONTRACT.code),
    //TODO 
    CONTRACT_PRODUCT(XbbRefTypeEnum.CONTRACT_PRODUCT,"tb_saas_contract_product",SubsidiaryEnum.RELATED_PRODUCT.getCode(),0),
    CONTRACT_PERFORMANCE(XbbRefTypeEnum.CONTRACT_PERFORMANCE,"tb_saas_contract_performance",SubsidiaryEnum.PERFORMANCE.getCode(),0),
    //
    REFUND(XbbRefTypeEnum.REFUND,"tb_saas_refund",SubsidiaryEnum.MASTER.getCode(),0),
    REFUND_TEAM(XbbRefTypeEnum.REFUND_TEAM,"tb_saas_refund_user",SubsidiaryEnum.USER.getCode(),REFUND.code),
    REFUND_SUBFORM(XbbRefTypeEnum.REFUND_SUBFORM,"tb_saas_refund_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),REFUND.code),
    //
    SALES_OPPORTUNITY(XbbRefTypeEnum.SALES_OPPORTUNITY,"tb_saas_opportunity",SubsidiaryEnum.MASTER.getCode(),0),
    SALES_OPPORTUNITY_TEAM(XbbRefTypeEnum.SALES_OPPORTUNITY_TEAM,"tb_saas_opportunity_user",SubsidiaryEnum.USER.getCode(),SALES_OPPORTUNITY.code),
    SALES_OPPORTUNITY_PRODUCT(XbbRefTypeEnum.SALES_OPPORTUNITY_PRODUCT,"tb_saas_opportunity_product",SubsidiaryEnum.RELATED_PRODUCT.getCode(),0),
    SALES_OPPORTUNITY_SUBFORM(XbbRefTypeEnum.SALES_OPPORTUNITY_SUBFORM,"tb_saas_opportunity_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),SALES_OPPORTUNITY.code),
    //
    CONTACT(XbbRefTypeEnum.CONTACT,"tb_saas_contact",SubsidiaryEnum.MASTER.getCode(),0),
    CONTACT_TEAM(XbbRefTypeEnum.CONTACT_TEAM,"tb_saas_contact_user",SubsidiaryEnum.USER.getCode(),CONTACT.code),
    CONTACT_SUBFORM(XbbRefTypeEnum.CONTACT_SUBFORM,"tb_saas_contact_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),CONTACT.code),
    //
    CUSTOMER_COMMUNICATE(XbbRefTypeEnum.CUSTOMER_COMMUNICATE,"tb_saas_customer_communicate",SubsidiaryEnum.MASTER.getCode(),0),
    CUSTOMER_COMMUNICATE_SUBFORM(XbbRefTypeEnum.CUSTOMER_COMMUNICATE_SUBFORM,"tb_saas_customer_communicate_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),CUSTOMER_COMMUNICATE.code),
    //
    SUPPLIER_COMMUNICATE(XbbRefTypeEnum.SUPPLIER_COMMUNICATE,"tb_saas_supplier_communicate",SubsidiaryEnum.MASTER.getCode(),0),
    SUPPLIER_COMMUNICATE_SUBFORM(XbbRefTypeEnum.SUPPLIER_COMMUNICATE_SUBFORM,"tb_saas_supplier_communicate_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),SUPPLIER_COMMUNICATE.code),
    //
    COMMUNICATE_PLAN(XbbRefTypeEnum.COMMUNICATE_PLAN,"tb_saas_communicate_plan",SubsidiaryEnum.MASTER.getCode(),0),
    COMMUNICATE_PLAN_TEAM(XbbRefTypeEnum.COMMUNICATE_PLAN_TEAM,"tb_saas_communicate_plan_user",SubsidiaryEnum.USER.getCode(),COMMUNICATE_PLAN.code),
    COMMUNICATE_PLAN_SUBFORM(XbbRefTypeEnum.COMMUNICATE_PLAN_SUBFORM,"tb_saas_communicate_plan_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),COMMUNICATE_PLAN.code),
    //
    PAYMENT(XbbRefTypeEnum.PAYMENT,"tb_saas_payment",SubsidiaryEnum.MASTER.getCode(),0),
    PAYMENT_TEAM(XbbRefTypeEnum.PAYMENT_TEAM,"tb_saas_payment_user",SubsidiaryEnum.USER.getCode(),PAYMENT.code),
    PAYMENT_SUBFORM(XbbRefTypeEnum.PAYMENT_SUBFORM,"tb_saas_payment_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),PAYMENT.code),
    //
    PAYMENT_SHEET(XbbRefTypeEnum.PAYMENT_SHEET,"tb_saas_payment_sheet",SubsidiaryEnum.MASTER.getCode(),0),
    PAYMENT_SHEET_TEAM(XbbRefTypeEnum.PAYMENT_SHEET_TEAM,"tb_saas_payment_sheet_user",SubsidiaryEnum.USER.getCode(),PAYMENT_SHEET.code),
    PAYMENT_SHEET_SUBFORM(XbbRefTypeEnum.PAYMENT_SHEET_SUBFORM,"tb_saas_payment_sheet_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),PAYMENT_SHEET.code),
    //
    PAY_PLAN(XbbRefTypeEnum.PAY_PLAN,"tb_saas_pay_plan",SubsidiaryEnum.MASTER.getCode(),0),
    PAY_PLAN_TEAM(XbbRefTypeEnum.PAY_PLAN_TEAM,"tb_saas_pay_plan_user",SubsidiaryEnum.USER.getCode(),PAY_PLAN.code),
    PAY_PLAN_SUBFORM(XbbRefTypeEnum.PAY_PLAN_SUBFORM,"tb_saas_pay_plan_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),PAY_PLAN.code),
    //
    PAY_SHEET(XbbRefTypeEnum.PAY_SHEET,"tb_saas_pay_sheet",SubsidiaryEnum.MASTER.getCode(),0),
    PAY_SHEET_TEAM(XbbRefTypeEnum.PAY_SHEET_TEAM,"tb_saas_pay_plan_sheet_user",SubsidiaryEnum.USER.getCode(),PAY_SHEET.code),
    PAY_SHEET_SUBFORM(XbbRefTypeEnum.PAY_SHEET_SUBFORM,"tb_saas_pay_plan_sheet_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),PAY_SHEET.code),
    //
    INVOICE(XbbRefTypeEnum.INVOICE,"tb_saas_invoice",SubsidiaryEnum.MASTER.getCode(),0),
    INVOICE_SUBFORM(XbbRefTypeEnum.INVOICE_SUBFORM,"tb_saas_invoice_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),INVOICE.code),
    //
    PURCHASE_INVOICE(XbbRefTypeEnum.PURCHASE_INVOICE,"tb_saas_purchase_invoice",SubsidiaryEnum.MASTER.getCode(),0),
    PURCHASE_INVOICE_SUBFORM(XbbRefTypeEnum.PURCHASE_INVOICE_SUBFORM,"tb_saas_purchase_invoice_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),PURCHASE_INVOICE.code),
    //
    SUPPLIER(XbbRefTypeEnum.SUPPLIER,"tb_saas_supplier",SubsidiaryEnum.MASTER.getCode(),0),
    SUPPLIER_TEAM(XbbRefTypeEnum.SUPPLIER_TEAM,"tb_saas_supplier_user",SubsidiaryEnum.USER.getCode(),SUPPLIER.code),
    SUPPLIER_SUBFORM(XbbRefTypeEnum.SUPPLIER_SUBFORM,"tb_saas_supplier_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),SUPPLIER.code),
    //
    SUPPLIER_CONTACT(XbbRefTypeEnum.SUPPLIER_CONTACT,"tb_saas_supplier_contact",SubsidiaryEnum.MASTER.getCode(),0),
    SUPPLIER_CONTACT_TEAM(XbbRefTypeEnum.SUPPLIER_CONTACT_TEAM,"tb_saas_supplier_contact_user",SubsidiaryEnum.USER.getCode(),SUPPLIER_CONTACT.code),
    SUPPLIER_CONTACT_SUBFORM(XbbRefTypeEnum.SUPPLIER_CONTACT_SUBFORM,"tb_saas_supplier_contact_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),SUPPLIER_CONTACT.code),
    SUPPLIER_PRODUCT(XbbRefTypeEnum.SUPPLIER_PRODUCT,"tb_saas_supplier_product",SubsidiaryEnum.RELATED_PRODUCT.getCode(),0),
    PURCHASE(XbbRefTypeEnum.PURCHASE,"tb_saas_purchase",SubsidiaryEnum.MASTER.getCode(),0),
    PURCHASE_TEAM(XbbRefTypeEnum.PURCHASE_TEAM,"tb_saas_purchase_user",SubsidiaryEnum.USER.getCode(),PURCHASE.code),
    PURCHASE_SUBFORM(XbbRefTypeEnum.PURCHASE_SUBFORM,"tb_saas_purchase_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),PURCHASE.code),
    PURCHASE_PRODUCT(XbbRefTypeEnum.PURCHASE_PRODUCT,"tb_saas_purchase_product",SubsidiaryEnum.RELATED_PRODUCT.getCode(),0),
    RETURNED_PURCHASE(XbbRefTypeEnum.RETURNED_PURCHASE,"tb_saas_returned_purchase",SubsidiaryEnum.MASTER.getCode(),0),
    RETURNED_PURCHASE_SUBFORM(XbbRefTypeEnum.RETURNED_PURCHASE_SUBFORM,"tb_saas_returned_purchase_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),RETURNED_PURCHASE.code),
    RETURNEDPURCHASE_PRODUCT(XbbRefTypeEnum.RETURNEDPURCHASE_PRODUCT,"tb_saas_returned_purchase_product",SubsidiaryEnum.RELATED_PRODUCT.getCode(),0),
//    INVENTORY_MANAGEMENT(XbbRefTypeEnum.INVENTORY_MANAGEMENT,"tb_saas_inventory",SubsidiaryEnum.MASTER.getCode(),0),
    INVENTORY(XbbRefTypeEnum.INVENTORY,"tb_saas_inventory",SubsidiaryEnum.MASTER.getCode(),0),
    INVENTORY_SUBFORM(XbbRefTypeEnum.INVENTORY_SUBFORM,"tb_saas_inventory_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),INVENTORY.code),
    INVENTORY_PRODUCT(XbbRefTypeEnum.INVENTORY_PRODUCT,"tb_saas_inventory_product",SubsidiaryEnum.RELATED_PRODUCT.getCode(),0),
    ASSEMBLE(XbbRefTypeEnum.ASSEMBLE,"tb_saas_assemble",SubsidiaryEnum.MASTER.getCode(),0),
    ASSEMBLE_PRODUCT(XbbRefTypeEnum.ASSEMBLE_PRODUCT,"tb_saas_assemble_product",SubsidiaryEnum.RELATED_PRODUCT.getCode(),0),
    ASSEMBLE_SUBFORM(XbbRefTypeEnum.ASSEMBLE_SUBFORM,"tb_saas_assemble_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),ASSEMBLE.code),
    INSTOCK(XbbRefTypeEnum.INSTOCK,"tb_saas_instock",SubsidiaryEnum.MASTER.getCode(),0),
    INSTOCK_PRODUCT(XbbRefTypeEnum.INSTOCK_PRODUCT,"tb_saas_instock_product",SubsidiaryEnum.RELATED_PRODUCT.getCode(),0),
    INSTOCK_SUBFORM(XbbRefTypeEnum.INSTOCK_SUBFORM,"tb_saas_instock_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),INSTOCK.code),
    /**
     * 以下几个和入库单在同一个表里
     */
    PURCHASE_INSTOCK(XbbRefTypeEnum.PURCHASE_INSTOCK,INSTOCK.tableName,INSTOCK.getSubsidiary(),0),
    REFUND_INSTOCK(XbbRefTypeEnum.REFUND_INSTOCK,INSTOCK.tableName,INSTOCK.getSubsidiary(),0),
    OTHER_INSTOCK(XbbRefTypeEnum.OTHER_INSTOCK,INSTOCK.tableName,INSTOCK.getSubsidiary(),0),
    PRODUCTION_INSTOCK(XbbRefTypeEnum.PRODUCTION_INSTOCK,INSTOCK.tableName,INSTOCK.getSubsidiary(),0),
    RETURNED_MATERIEL_INSTOCK(XbbRefTypeEnum.RETURNED_MATERIEL_INSTOCK,INSTOCK.tableName,INSTOCK.getSubsidiary(),0),
    //出库单
    OUTSTOCK(XbbRefTypeEnum.OUTSTOCK,"tb_saas_outstock",SubsidiaryEnum.MASTER.getCode(),0),
    OUTSTOCK_SUBFORM(XbbRefTypeEnum.OUTSTOCK_SUBFORM,"tb_saas_outstock_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),0),
    OUTSTOCK_PRODUCT(XbbRefTypeEnum.OUTSTOCK_PRODUCT,"tb_saas_outstock_product",SubsidiaryEnum.RELATED_PRODUCT.getCode(),0),
    CONTRACT_OUTSTOCK(XbbRefTypeEnum.CONTRACT_OUTSTOCK,OUTSTOCK.tableName,OUTSTOCK.getSubsidiary(),0),
    RETURNED_PURCHASE_OUTSTOCK(XbbRefTypeEnum.RETURNED_PURCHASE_OUTSTOCK,OUTSTOCK.tableName,OUTSTOCK.getSubsidiary(),0),
    OTHER_OUTSTOCK(XbbRefTypeEnum.OTHER_OUTSTOCK,OUTSTOCK.tableName,OUTSTOCK.getSubsidiary(),0),
    PRODUCTION_MATERIEL_OUTSTOCK(XbbRefTypeEnum.PRODUCTION_MATERIEL_OUTSTOCK,OUTSTOCK.tableName,OUTSTOCK.getSubsidiary(),0),
    WORK_ORDER_OUTSTOCK(XbbRefTypeEnum.WORK_ORDER_OUTSTOCK,OUTSTOCK.tableName,OUTSTOCK.getSubsidiary(),0),
    //
    TRANSFER(XbbRefTypeEnum.TRANSFER,"tb_saas_transfer",SubsidiaryEnum.MASTER.getCode(),0),
    TRANSFER_PRODUCT(XbbRefTypeEnum.TRANSFER_PRODUCT,"tb_saas_transfer_product",SubsidiaryEnum.RELATED_PRODUCT.getCode(),0),
    TRANSFER_SUBFORM(XbbRefTypeEnum.TRANSFER_SUBFORM,"tb_saas_transfer_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),TRANSFER.code),
    
    WAREHOUSE(XbbRefTypeEnum.WAREHOUSE,"tb_saas_warehouse",SubsidiaryEnum.MASTER.getCode(),0),
//    WAREHOUSE_PRODUCT(XbbRefTypeEnum.WAREHOUSE_PRODUCT,"",0),
    WAREHOUSE_TEAM(XbbRefTypeEnum.WAREHOUSE_TEAM,"tb_saas_warehouse_user",SubsidiaryEnum.USER.getCode(),WAREHOUSE.code),
    WAREHOUSE_SUBFORM(XbbRefTypeEnum.WAREHOUSE_SUBFORM,"tb_saas_warehouse_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),WAREHOUSE.code),
//    WORK_REPORT(XbbRefTypeEnum.WORK_REPORT,"tb_saas_work_report",0),
//    WORKREPORT_DAILY(XbbRefTypeEnum.WORKREPORT_DAILY,"",0),
//    WORKREPORT_WEEKLY(XbbRefTypeEnum.WORKREPORT_WEEKLY,"",0),
//    WORKREPORT_MONTHLY(XbbRefTypeEnum.WORKREPORT_MONTHLY,"",0),
//    WORKREPORT_STATISIC(XbbRefTypeEnum.WORKREPORT_STATISIC,"",0),
//    WORK_REPORT_SUBFORM(XbbRefTypeEnum.WORK_REPORT_SUBFORM,"",0),
    WORK_ORDER(XbbRefTypeEnum.WORK_ORDER,"tb_saas_work_order_data",SubsidiaryEnum.MASTER.getCode(),0),
    WORK_ORDER_SUBFORM(XbbRefTypeEnum.WORK_ORDER_SUBFORM,"tb_saas_work_order_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),WORK_ORDER.code),
    WORK_ORDER_TEAM(XbbRefTypeEnum.WORK_ORDER_TEAM,"tb_work_order_flow_user",SubsidiaryEnum.USER.getCode(),WORK_ORDER.code),
//    WORK_ORDER_CC(XbbRefTypeEnum.WORK_ORDER_CC,"",0),

    PRODUCT(XbbRefTypeEnum.PRODUCT,"tb_saas_product",SubsidiaryEnum.MASTER.getCode(),0),
    PRODUCT_TEAM(XbbRefTypeEnum.PRODUCT_TEAM,"tb_saas_product_user",SubsidiaryEnum.USER.getCode(),PRODUCT.code),
    PRODUCT_SUBFORM(XbbRefTypeEnum.PRODUCT_SUBFORM,"tb_saas_product_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),PRODUCT.code),
    BOM_BILL(XbbRefTypeEnum.BOM_BILL,"tb_saas_bom",SubsidiaryEnum.MASTER.getCode(),0),
    BOM_BILL_PRODUCT(XbbRefTypeEnum.BOM_BILL_PRODUCT,"tb_saas_bom_product",SubsidiaryEnum.RELATED_PRODUCT.getCode(),0),
    BOM_BILL_TEAM(XbbRefTypeEnum.BOM_BILL_TEAM,"tb_saas_bom_user",SubsidiaryEnum.USER.getCode(),BOM_BILL.code),
    BOM_BILL_SUBFORM(XbbRefTypeEnum.BOM_BILL_SUBFORM,"tb_saas_bom_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),BOM_BILL.code),
    PRODUCTION_ORDER(XbbRefTypeEnum.PRODUCTION_ORDER,"tb_saas_production_order",SubsidiaryEnum.MASTER.getCode(),0),
    PRODUCTION_ORDER_TEAM(XbbRefTypeEnum.PRODUCTION_ORDER_TEAM,"tb_saas_production_order_user",SubsidiaryEnum.USER.getCode(),PRODUCTION_ORDER.code),
    PRODUCTION_ORDER_SUBFORM(XbbRefTypeEnum.PRODUCTION_ORDER_SUBFORM,"tb_saas_production_order_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),PRODUCTION_ORDER.code),
    PRODUCTION_ORDER_PRODUCT(XbbRefTypeEnum.PRODUCTION_ORDER_PRODUCT,"tb_saas_production_order_product",SubsidiaryEnum.RELATED_PRODUCT.getCode(),0),
    QUOTATION(XbbRefTypeEnum.QUOTATION,"tb_saas_quotation",SubsidiaryEnum.MASTER.getCode(),0),
    QUOTATION_TEAM(XbbRefTypeEnum.QUOTATION_TEAM,"tb_saas_quotation_user",SubsidiaryEnum.USER.getCode(),QUOTATION.code),
    QUOTATION_PRODUCT(XbbRefTypeEnum.QUOTATION_PRODUCT,"tb_saas_quotation_product",SubsidiaryEnum.RELATED_PRODUCT.getCode(),0),
    QUOTATION_SUBFORM(XbbRefTypeEnum.QUOTATION_SUBFORM,"tb_saas_quotation_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),QUOTATION.code),
    REIMBURSEMENT_FORM(XbbRefTypeEnum.REIMBURSEMENT_FORM,"tb_saas_reimbursement_form",SubsidiaryEnum.MASTER.getCode(),0),
    EXPENSE(XbbRefTypeEnum.EXPENSE,"tb_saas_expense",SubsidiaryEnum.MASTER.getCode(),0),
    CLUE(XbbRefTypeEnum.CLUE,"tb_saas_clue",SubsidiaryEnum.MASTER.getCode(),0),
    CLUE_TEAM(XbbRefTypeEnum.CLUE_TEAM,"tb_saas_clue_user",SubsidiaryEnum.USER.getCode(),CLUE.code),
    CLUE_SUBFORM(XbbRefTypeEnum.CLUE_SUBFORM,"tb_saas_clue_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),CLUE.code),
    MARKET_ACTIVITY(XbbRefTypeEnum.MARKET_ACTIVITY,"tb_saas_market_activity",SubsidiaryEnum.MASTER.getCode(),0),
    MARKET_ACTIVITY_TEAM(XbbRefTypeEnum.MARKET_ACTIVITY_TEAM,"tb_saas_market_activity_user",SubsidiaryEnum.USER.getCode(),MARKET_ACTIVITY.code),
    MARKET_ACTIVITY_SUBFORM(XbbRefTypeEnum.MARKET_ACTIVITY_SUBFORM,"tb_saas_market_activity_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),MARKET_ACTIVITY.code),
    OTHER_INCOME(XbbRefTypeEnum.OTHER_INCOME,"tb_saas_other_income",SubsidiaryEnum.MASTER.getCode(),0),
    OTHER_INCOME_SUBFORM(XbbRefTypeEnum.OTHER_INCOME_SUBFORM,"tb_saas_other_income_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),OTHER_INCOME.code),
    OTHER_EXPENSE(XbbRefTypeEnum.OTHER_EXPENSE,"tb_saas_other_expense",SubsidiaryEnum.MASTER.getCode(),0),
    OTHER_EXPENSE_SUBFORM(XbbRefTypeEnum.OTHER_EXPENSE_SUBFORM,"tb_saas_other_expense_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),OTHER_EXPENSE.code),

    WORK_ORDER_V2(XbbRefTypeEnum.WORK_ORDER_V2,"tb_work_order_v2_data",SubsidiaryEnum.MASTER.getCode(),0),
    WORK_ORDER_V2_TEAM(XbbRefTypeEnum.WORK_ORDER_V2_TEAM, "tb_work_order_v2_user", SubsidiaryEnum.USER.getCode(), WORK_ORDER_V2.code),
    WORK_ORDER_V2_SUBFORM(XbbRefTypeEnum.WORK_ORDER_V2_SUBFORM,"tb_work_order_v2_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),0),
    WORK_ORDER_V2_PRODUCT(XbbRefTypeEnum.WORK_ORDER_V2_PRODUCT,"tb_work_order_v2_product",SubsidiaryEnum.RELATED_PRODUCT.getCode(),0),
    WORK_ORDER_V2_NODE_LOG_ITEM(XbbRefTypeEnum.WORK_ORDER_V2_NODE_INFO_LOG_ITEM,"tb_work_order_v2_node_log_item",SubsidiaryEnum.MASTER.getCode(),0),
    WORK_ORDER_V2_NODE_INFO(XbbRefTypeEnum.WORK_ORDER_V2_NODE_INFO, "tb_work_order_v2_node_log_info", SubsidiaryEnum.MASTER.getCode(), 0),

    //SLA日志,
    SLA_LOG(XbbRefTypeEnum.SLA_LOG,"tb_sla_log",SubsidiaryEnum.MASTER.getCode(),0),
    SLA_LOG_TEAM(XbbRefTypeEnum.SLA_LOG_TEAM, "tb_sla_log_user", SubsidiaryEnum.USER.getCode(), SLA_LOG.code),
    //服务项目
    SERVICE_PROJECT(XbbRefTypeEnum.SERVICE_PROJECT,"tb_work_order_v2_service_project",SubsidiaryEnum.MASTER.getCode(),0),
    SERVICE_PROJECT_TEAM(XbbRefTypeEnum.SERVICE_PROJECT_TEAM, "tb_work_order_v2_service_project_user", SubsidiaryEnum.USER.getCode(), SERVICE_PROJECT.code),
    SERVICE_PROJECT_SUBFORM(XbbRefTypeEnum.SERVICE_PROJECT_SUBFORM,"tb_work_order_v2_service_project_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),SERVICE_PROJECT.code),
    //回执单
    RECEIPT_ORDER(XbbRefTypeEnum.RECEIPT_ORDER,"tb_work_order_v2_receipt_order",SubsidiaryEnum.MASTER.getCode(),0),
    RECEIPT_ORDER_TEAM(XbbRefTypeEnum.RECEIPT_ORDER_TEAM, "tb_work_order_v2_receipt_order_user", SubsidiaryEnum.USER.getCode(), RECEIPT_ORDER.code),
    RECEIPT_ORDER_SUBFORM(XbbRefTypeEnum.RECEIPT_ORDER_SUBFORM,"tb_work_order_v2_receipt_sub_data",SubsidiaryEnum.SUB_DATA.getCode(),0),
    RECEIPT_ORDER_PRODUCT(XbbRefTypeEnum.RECEIPT_ORDER_PRODUCT,"tb_work_order_v2_receipt_order_product",SubsidiaryEnum.RELATED_PRODUCT.getCode(),0),

    RECEIPT_ORDER_SERVICE(XbbRefTypeEnum.RECEIPT_ORDER_SERVICE,"tb_work_order_v2_receipt_order_service",SubsidiaryEnum.MASTER.getCode(),0),

    //自定义图表
    WORK_ORDER_V2_NODE_LOG_INFO(XbbRefTypeEnum.WORK_ORDER_V2_NODE_LOG_INFO, "tb_work_order_v2_node_log_info", SubsidiaryEnum.MASTER.getCode(), 0),

    COMPETITOR(XbbRefTypeEnum.COMPETITOR,"tb_saas_competitor",SubsidiaryEnum.MASTER.getCode(),0),
    COMPETITOR_TEAM(XbbRefTypeEnum.COMPETITOR_TEAM,"tb_saas_competitor_user",SubsidiaryEnum.USER.getCode(), COMPETITOR.getCode()),
    COMPETITOR_SUBFORM(XbbRefTypeEnum.COMPETITOR_SUBFORM,"tb_saas_competitor_sub_data",SubsidiaryEnum.SUB_DATA.getCode(), COMPETITOR.getCode()),
    UNKNOWN(XbbRefTypeEnum.UNKNOWN,"",SubsidiaryEnum.MASTER.getCode(),0),
    ;
    /**
     * 枚举值 --
     */
    private final int code;

    /**
     * 枚举信息--中文名称
     */
    private final String name;

    /**
     * 枚举描述 --英文名称
     */
    private final String alias;
    /**
     * 枚举描述 --对应的数据库表名称
     */
    private final String tableName;
    /**
     * 主表、从表、团队
     */
    private final int subsidiary;
    /**
     * 父表的code
     */
    private final int parentCode;
    

    XbbTableRefEnum(int code, String name, String alias, String tableName,int subsidiary,int parentCode) {
        this.code = code;
        this.name = name;
        this.alias = alias;
        this.tableName = tableName;
        this.subsidiary = subsidiary;
        this.parentCode = parentCode;
    }


    XbbTableRefEnum(XbbRefTypeEnum xbbRefTypeEnum, String tableName,int subsidiary,int parentCode) {
        this.code = xbbRefTypeEnum.getCode();
        this.name = xbbRefTypeEnum.getName();
        this.alias = xbbRefTypeEnum.getAlias();
        this.tableName = tableName;
        this.subsidiary = subsidiary;
        this.parentCode = parentCode;
    }


    /**
     * 通过业务类型获取枚举
     * @param code
     * @return
     */
    public static XbbTableRefEnum getByCode(int code){
        if(Objects.equals(0,code)){
            return PAAS;
        }
        for (XbbTableRefEnum cacheCode : values()) {
            if (Objects.equals(code, cacheCode.getCode())) {
                return cacheCode;
            }
        }
        //TODO 是否要返回null
        return UNKNOWN;
    }
    /**
     * 通过业务类型获取枚举
     * @param code
     * @return
     */
    public static XbbTableRefEnum getByCode(int code, int saasMark){
        if(Objects.equals(saasMark, SaasMarkEnum.PAAS.getCode()) || Objects.equals(0,code)){
            return PAAS;
        }
        for (XbbTableRefEnum cacheCode : values()) {
            if (Objects.equals(code, cacheCode.getCode())) {
                return cacheCode;
            }
        }
        //TODO 是否要返回null
        return UNKNOWN;
    }

    /**
     * 表名称集合
     */
    static Set<String> tableNameSet = new HashSet<>(XbbConstant.INITIALCAPACITY);
    public static Set<String> getTableNames() {
        for (XbbTableRefEnum value : values()) {
            String tableName = value.getTableName();
            //没有标记的暂时先不移动进来
            if(!StringUtils.isEmpty(tableName)){
                tableNameSet.add(tableName);
            }
        }
        return tableNameSet;
    }

    /**
     * 团队表名称集合
     */
    static Set<String> userTableNameSet = new HashSet<>(XbbConstant.INITIALCAPACITY);
    /**
     * 团队表枚举集合
     */
    static Set<XbbTableRefEnum> userTableEnumSet = new HashSet<>(XbbConstant.INITIALCAPACITY);
    /**
     * 团队表相关表名集合
     */
    static Set<String> userTableCorrNameSet = new HashSet<>(XbbConstant.INITIALCAPACITY);
    public static Set<String> getUserTableNames() {
        return getUserTableNames(null);
    }
    public static Set<String> getUserTableNames(String pref) {
        for (XbbTableRefEnum value : values()) {
            String tableName = value.getTableName();
            //没有标记的暂时先不移动进来
            if(!StringUtils.isEmpty(tableName) && Objects.equals(SubsidiaryEnum.USER.getCode(),value.getSubsidiary())){
                if(!StringUtils.isEmpty(pref)){
                    tableName = pref + tableName;
                }
                userTableNameSet.add(tableName);
            }
        }
        return userTableNameSet;
    }
    
    
    public static Set<XbbTableRefEnum> getUserTableEnums() {
        for (XbbTableRefEnum value : values()) {
            String tableName = value.getTableName();
            //没有标记的暂时先不移动进来
            if(!StringUtils.isEmpty(tableName) && Objects.equals(SubsidiaryEnum.USER.getCode(),value.getSubsidiary())){
                userTableEnumSet.add(value);
            }
        }
        return userTableEnumSet;
    }

    /**
     * 根据parentCode获取团队枚举
     * @param parentCode
     * @return
     */
    public static XbbTableRefEnum getUserTableEnumByParentCode(int parentCode) {
        // paas 表单的businessType 有些地方穿成了0,不是4 两者默认等同，后面应该强制传4
        if (Objects.equals(parentCode, BasicConstant.ZERO)) {
            return PAAS_TEAM;
        }
        for (XbbTableRefEnum value : values()) {
            String tableName = value.getTableName();
            //没有标记的暂时先不移动进来
            if(!StringUtils.isEmpty(tableName) && Objects.equals(SubsidiaryEnum.USER.getCode(),value.getSubsidiary()) && Objects.equals(value.getParentCode(),parentCode) ){
                return value;
            }
        }
        return null;
    }

    /**
     * 根据主表的businessType获取对应的团队表名称
     * @param businessType
     * @return
     */
    public static String getTeamNameByParent(Integer businessType) {
        XbbTableRefEnum xbbTableRefEnum = getUserTableEnumByParentCode(businessType);
        if(xbbTableRefEnum != null){
            String tableName = xbbTableRefEnum.getTableName();
//            tableName = tableName + XbbConstant.TEAM;
            return tableName;
        }
        return null;
    }
    
    /**
     * 根据parentCode获取子表单枚举
     * @param parentCode
     * @return
     */
    public static XbbTableRefEnum getSubFormTableEnumByParentCode(int parentCode) {
        //TODO  paas 表单的businessType 有些地方穿成了0,不是4 两者默认等同，后面应该强制传4
        if (Objects.equals(parentCode, BasicConstant.ZERO)) {
            return PAAS_SUBFORM;
        }
        int parentCodeAlias = parentCode;
        for (XbbTableRefEnum value : values()) {
            String tableName = value.getTableName();
            //入库单和出库单的表相同
            if(Objects.equals(tableName,INSTOCK_SUBFORM.tableName)){
                parentCodeAlias  = INSTOCK.code;
            }else if(Objects.equals(tableName,OUTSTOCK_SUBFORM.tableName)){
                parentCodeAlias  = OUTSTOCK.code;
            }
            //没有标记的暂时先不移动进来
            if(!StringUtils.isEmpty(tableName) && Objects.equals(SubsidiaryEnum.SUB_DATA.getCode(),value.getSubsidiary()) && Objects.equals(value.getParentCode(),parentCodeAlias) ){
                return value;
            }
        }
        return null;
    }

    /**
     * 获取需要转换的表名
     * @return
     */
    public static Set<String> getUserTableCorrNameSet() {
        for (XbbTableRefEnum value : values()) {
            String tableName = value.getTableName();
            //没有标记的暂时先不移动进来
            if(!StringUtils.isEmpty(tableName) && Objects.equals(SubsidiaryEnum.USER.getCode(),value.getSubsidiary())){
                userTableCorrNameSet.add(tableName + XbbConstant.SINK);
                userTableCorrNameSet.add(tableName + XbbConstant.TEAM);
            }
        }
        return userTableCorrNameSet;
    }

    static Set<String> transfTableNameSet = new HashSet<>();

    /**
     * 获取需要转换的表名
     * @param pref
     * @return
     */
    public static Set<String> getTransfTableNames(String pref) {
        for (XbbTableRefEnum value : values()) {
            String tableName = value.getTableName();
            //没有标记的暂时先不移动进来
            if(!StringUtils.isEmpty(tableName) && (Objects.equals(SubsidiaryEnum.USER.getCode(),value.getSubsidiary())
                    || Objects.equals(SubsidiaryEnum.SUB_DATA.getCode(),value.getSubsidiary()) 
                    || Objects.equals(SubsidiaryEnum.RELATED_PRODUCT.getCode(),value.getSubsidiary())
                    || Objects.equals(SubsidiaryEnum.PERFORMANCE.getCode(),value.getSubsidiary()))){
                if(!StringUtils.isEmpty(pref)){
                    tableName = pref + tableName;
                }
                transfTableNameSet.add(tableName);
            }
        }
        return transfTableNameSet;
    }

    /**
     * 根据主表的businessType获取对应的子表单表名称
     * @param businessType
     * @return
     */
    public static String getSubFormNameByParent(Integer businessType) {
        XbbTableRefEnum xbbTableRefEnum = XbbTableRefEnum.getSubFormTableEnumByParentCode(businessType);
        if(xbbTableRefEnum != null){
            String tableName = xbbTableRefEnum.getTableName();
//            tableName = tableName + XbbConstant.TEAM;
            return tableName;
        }
        return null;
    }

    /**
     * 通过name获取枚举
     * @param tableName
     * @return
     */
    public static XbbTableRefEnum getByTable(String tableName){
        for (XbbTableRefEnum xbbTableRefEnum : values()) {
            if (Objects.equals(tableName, xbbTableRefEnum.getTableName())) {
                return xbbTableRefEnum;
            }
        }
        //TODO 是否要返回null
        return UNKNOWN;
    }

    /**
     * BI是否支持指定业务
     *
     * @param xbbRefTypeEnum
     * @return boolean
     * @author zyy
     * @date 2022/11/15
     * @version v1.0
     */
    public static boolean supportBi(XbbRefTypeEnum xbbRefTypeEnum){
        if(Objects.equals(xbbRefTypeEnum.getCode(),UNKNOWN.code)){
            return false;
        }
        for (XbbTableRefEnum xbbTableRefEnum : values()) {
            if(Objects.equals(xbbTableRefEnum.code,xbbRefTypeEnum.getCode())){
                return true;
            }
        }
        return false;
    }

}
